package com.foreveredu.furedusys.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.furedusys.domain.Role;
import com.foreveredu.furedusys.service.ISystemService;
import com.foreveredu.furedusys.web.struts.actionform.RoleActionForm;

public class RoleAction extends AbstractBaseDispatchAction {

	private ISystemService systemService = null;

	public ISystemService getsystemService() {
		return systemService;
	}

	public void setsystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Role> roles = systemService.findRoles();
		request.setAttribute("roles", roles);
		return mapping.findForward("list");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleActionForm raf = (RoleActionForm) form;
		systemService.deleteRoleById(raf.getId());
		return mapping.findForward("delete");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleActionForm raf = (RoleActionForm) form;
		Role role = systemService.findRoleById(raf.getId());
		request.setAttribute("role", role);
		return mapping.findForward("toUpdate");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleActionForm raf = (RoleActionForm) form;
		Role role = new Role();
		PropertyUtils.copyProperties(role, raf);
		if(raf.getName()==null || "".equals(raf.getName()) ){
			request.setAttribute("role", role);
			request.setAttribute("error","角色名不能为空！");
			return mapping.findForward("save");
		}
		if(raf.getDescription()==null || "".equals(raf.getDescription())){
			request.setAttribute("error","描述信息不能为空！");
			request.setAttribute("role", role);
			return mapping.findForward("save");
		}
		systemService.saveRole(role);
		return mapping.findForward("save");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleActionForm raf = (RoleActionForm) form;
		Role role = systemService.findRoleById(raf.getId());
		role.setName(raf.getName());
		role.setDescription(raf.getDescription());
		if(raf.getName()==null || "".equals(raf.getName()) ){
			request.setAttribute("role", role);
			request.setAttribute("error","角色名不能为空！");
			return mapping.findForward("toUpdate");
		}
		if(raf.getDescription()==null || "".equals(raf.getDescription())){
			request.setAttribute("error","描述信息不能为空！");
			request.setAttribute("role", role);
			return mapping.findForward("toUpdate");
		}
		systemService.updateRole(role);
		return mapping.findForward("update");
	}

}
